/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.internal.model;

import java.text.MessageFormat;

import net.sourceforge.perforce.core.resources.IP4File;
import net.sourceforge.perforce.ui.model.PerforceModelElement;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IActionFilter;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
  * @version $Revision: 175 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class P4FileElement extends PerforceModelElement implements IWorkbenchAdapter, IActionFilter
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Message format for creating the label. */
	private MessageFormat format = new MessageFormat("{0}#{1} <{2}>");
	
	// ---------------------------------------------------------- Public Methods (IWorkbenchAdapter)

	/** @see IWorkbenchAdapter#getChildren(Object) */
	public Object[] getChildren(Object o)
	{
		return new Object[0];
	}

	/** @see IWorkbenchAdapter#getImageDescriptor(Object) */
	public ImageDescriptor getImageDescriptor(Object object)
	{
		if (!(object instanceof IP4File)) 
		{
			return null;
		}
		
		IP4File file = (IP4File) object;
		return PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor(file.getName());
	}

	/** @see IWorkbenchAdapter#getLabel(Object) */
	public String getLabel(Object object)
	{
		if (!(object instanceof IP4File)) 
		{
			return null;
		}
		
		IP4File file = (IP4File) object;
		Object[] arguments = new Object[] 
		{ 
			file.getDepotFilename(), 
			file.getRevision(), 
			file.getAction() 
		};
		
		return format.format(arguments);
	}

	/** @see IWorkbenchAdapter#getParent(Object) */
	public Object getParent(Object o)
	{
		return null;
	}
	
	// -------------------------------------------------------------- Public Methods (IActionFilter)
	
	/** @see IActionFilter#testAttribute(Object, String, String) */
	public boolean testAttribute(Object target, String name, String value) 
	{
		if (name.equals("clientName"))                                                 //$NON-NLS-1$
		{
			boolean expectedValue = !value.equalsIgnoreCase("other");
			IP4File changelist = (IP4File) target;
			
			boolean realValue = 
				changelist.getClientName().equals(changelist.getServer().getClient());
			return realValue == expectedValue;
			
		}
		return false;
	}		
}
